'use strict';
(function (exports) {
    var $ = null;

    var PhzRoomQuit = cc.Layer.extendEX({
        getRootNode: function () {
            return this.getChildByName('Scene');
        },

        ctor: function () {
            this._super();
            var that = this;

            $ = create$(loadNodeCCS(res.PhzRoomQuit_json, this));

            this.bt1 = $('root.panel.btn_confirm');
            var canTouch = false;
            TouchUtil.setOnclickListener(this.bt1, function () {
                if (!canTouch)
                    return;
                network.sendPhz(5000, 'Vote/quit/1/0/0');
                that.isVote = true;
                that.updateBT();
            });
            this.bt2 = $('root.panel.btn_cancel');
            TouchUtil.setOnclickListener(this.bt2, function () {
                if (!canTouch)
                    return;
                network.sendPhz(5000, 'Vote/quit/2/0/0');
                that.isVote = true;
                that.updateBT();
            });

            var posArr1 = [cc.p(383, 366), cc.p(644, 366), cc.p(905, 366)];
            var posArr2 = [cc.p(303, 366), cc.p(531, 366), cc.p(758, 366), cc.p(985, 366)];
            var length = gameData.players.length;
            if (length == 3) {
                for (var i = 0; i < length; i++) {
                    $('root.info' + i).setPosition(posArr1[i]);
                }
                $('root.info' + 3).setVisible(false);
            } else {
                for (var i = 0; i < length; i++) {
                    $('root.info' + i).setPosition(posArr2[i]);
                }
                $('root.info' + 3).setVisible(true);
            }

            this.t1 = $('root.info0.status');
            this.t1 = $('root.info0.status');
            this.t2 = $('root.info1.status');
            this.t3 = $('root.info2.status');
            this.t4 = $('root.info3.status');
            this.statusicon1 = $('root.info0.statusicon');
            this.statusicon2 = $('root.info1.statusicon');
            this.statusicon3 = $('root.info2.statusicon');
            this.statusicon4 = $('root.info3.statusicon');
            this.t0 = $('root.panel.t0');
            this.txtTime = $('root.panel.txtTime');

            this.result = $('root.panel.result');
            this.btn_ok = $('root.panel.btn_ok');
            this.result.setVisible(false);
            this.btn_ok.setVisible(false);

            mRoom.isPause = true;
            this.timeLeft = 90;
            this.txtTime.setString(this.timeLeft);
            this.schedule(this.onTimer, 1);

            this.isVote = false;
            this.updateBT();

            //cc.eventManager.addCustomListener(P.GS_Vote, this.onGameVote.bind(this));

            //头像
            var pList = gameData.players;
            for (var i = 0; i < pList.length; i++) {
                var player = pList[i];
                //头像
                var avator = $('root.info' + (i) + '.head');
                var url = decodeURIComponent(player.HeadIMGURL);
                if (url == undefined || (url.length == 0)) url = 'res/defaultHead.jpg';
                loadImageToSprite(url, avator);//头像
            }
            if (gameData.switchVideo) {
                AgoraUtil.hideAllVideo();
            }
            //几人玩法
            if (gameData.playerNum <= 3) {
                this.t4.setVisible(false);
                $('root.info' + (3)).setVisible(false);
            }
            if (gameData.playerNum <= 2) {
                this.t3.setVisible(false);
                $('root.info2').setVisible(false);
            }
            var panel = $('root.panel');
            panel.setScale(0);
            panel.runAction(cc.sequence(
                cc.scaleTo(0.1, 0.8, 1.2),
                cc.scaleTo(0.08, 1.1, 0.9),
                cc.scaleTo(0.05, 0.95, 1.05),
                cc.scaleTo(0.05, 1, 1),
                cc.callFunc(function () {
                    canTouch = true;
                })
            ));
            return true;
        },

        updateBT: function () {
            this.bt1.setVisible(!this.isVote);
            this.bt2.setVisible(!this.isVote);
        },
        onTimer: function (dt) {
            var that = this;
            this.timeLeft -= 1;
            if (this.timeLeft <= 0) {
                this.timeLeft = 0;
                if (this.isVote == false) {
                    this.isVote = true;
                    network.sendPhz(5000, 'vote/quit/2');
                }
                // mRoom.isPause = false;
                // that.removeFromParent();
            }
            this.txtTime.setString(this.timeLeft);

        },

        onGameVote: function (event) {
            var that = this;
            //出牌
            var data = event.getUserData();
            if (!data.Voting) {
                this.timeLeft = 0;
                gameData.jiesanSucess = true;
                TouchUtil.setOnclickListener(this.btn_ok, function () {
                    that.removeFromParent();
                    mRoom.isPause = false;
                    if (gameData.switchVideo) {
                        AgoraUtil.showAllVideo();
                    }
                });
                $('root.panel.btn_confirm').setVisible(false);
                $('root.panel.btn_cancel').setVisible(false);
                $('root.panel.btn_ok').setVisible(true);
                $('root.panel.result').setVisible(true);
                $('root.panel.result').setString('投票结果为: 解散房间');
                return;
            }
            this.setData(data, false);
        },

        setData: function (data, isCreate, parent) {
            var that = this;
            if (parent) {
                this.parentlayer = parent;
            }
            if (data.Voting) {
                if (this.timeLeft == 0) {
                    this.timeLeft = 90;
                    //this.txtTime2.setVisible(true);
                }
                var leftSeconds = data.EndSecond;
                var voteResult = data.VoteResult;
                leftSeconds = (leftSeconds < 0 ? 0 : leftSeconds);
                //this.timeLeft = leftSeconds;
                that.setArr(leftSeconds, data.Users, data.ByUserID);
            }
            else {
                var p = data.Content.split('/');

                var userId = parseInt(p[0]);
                var vote = parseInt(p[3]);

                if (isCreate == true) {
                    this.players = {};

                    var pList = gameData.players;
                    for (var i = 0; i < pList.length; i++) {
                        var player = pList[i];

                        var info = {};
                        info.id = player.ID;
                        info.name = player.NickName;
                        info.vote = 0;
                        var txt = this['t' + (i + 1)];
                        var icon = this['statusicon' + (i + 1)];
                        info.txt = txt;
                        info.icon = icon;
                        txt.setString('未选择');
                        // icon.setTexture('res/tishi/roomjiesan_wait.png');
                        icon.setTexture(huzi.res.roomjiesan_wait_png);
                        this.players[info.id] = info;

                        if (info.id == userId) {
                            this.t0.setString('玩家［' + info.name + '］申请解散房间，请问是否同意？');
                            txt.setString('已同意');
                            icon.setTexture(huzi.res.roomjiesan_ok_png);
                            info.vote = 1;
                        }
                    }

                    if (userId == gameData.uid) {
                        this.isVote = true;
                        this.updateBT();
                    }
                } else {
                    this.players = {};

                    var pList = gameData.players;
                    for (var i = 0; i < pList.length; i++) {
                        var player = pList[i];

                        var info = {};
                        info.id = player.ID;
                        info.name = player.NickName;
                        info.vote = 0;
                        var txt = this['t' + (i + 1)];
                        var icon = this['statusicon' + (i + 1)];
                        info.txt = txt;
                        info.icon = icon;
                        this.players[info.id] = info;
                    }
                    var info = this.players[userId];
                    if (info != undefined) {
                        var txt = info.txt;
                        var icon = info.icon;
                        info.vote = vote;
                        if (info.vote == 1) {
                            txt.setString('已同意');
                            icon.setTexture(huzi.res.roomjiesan_ok_png);
                        } else if (info.vote == 2) {
                            txt.setString('已拒绝');
                            icon.setTexture(huzi.res.roomjiesan_no_png);
                        }
                    }
                }

                var acceptCount = 0;
                var rejectCount = 0;
                for (var key in this.players) {
                    var info = this.players[key];

                    if (info.vote == 1) {
                        acceptCount++;
                    } else if (info.vote == 2) {
                        rejectCount++;
                    }
                }

                if (acceptCount >= gameData.playerNum) {
                    gameData.jiesanSucess = true;
                    this.result.setVisible(true);
                    this.result.setString('投票结果为: 解散房间');
                    this.btn_ok.setVisible(true);
                    TouchUtil.setOnclickListener(this.btn_ok, function () {
                        if (that.parentlayer) {
                            that.parentlayer.showRoomClean();
                        }

                        // if(that.parentlayer){
                        //     if(that.parentlayer.gameResultLayer.isVisible() || that.parentlayer.resultLayer.isVisible() || that.parentlayer.isReady){
                        //         that.parentlayer.showRoomClean();
                        //     }
                        // }
                        that.removeFromParent();
                    });
                    this.bt1.setVisible(false);
                    this.bt2.setVisible(false);
                } else if (rejectCount >= 1) {
                    this.result.setVisible(true);
                    this.result.setString('投票结果为: 不解散房间');
                    this.btn_ok.setVisible(true);
                    TouchUtil.setOnclickListener(this.btn_ok, function () {
                        that.removeFromParent();
                        mRoom.isPause = false;
                        if (gameData.switchVideo) {
                            AgoraUtil.showAllVideo();
                        }
                    });
                    this.bt1.setVisible(false);
                    this.bt2.setVisible(false);
                }
            }
        },
        setArr: function (leftSeconds, arr, userId) {
            var that = this;
            var haveISelected = false;
            for (var i = 0; i < arr.length; i++) {
                if (arr[i].UserID == gameData.uid && (arr[i].VoteValue == 1 || arr[i].VoteValue == 2)) {
                    haveISelected = true;
                    break;
                }
            }
            var nickname = '';
            if (!!gameData && !!gameData.playerMap && !!gameData.playerMap[userId]) {
                nickname = gameData.playerMap[userId].NickName;
            }
            var content = '玩家【' + nickname + '】申请解散房间, ' + (haveISelected ? '请等待其他玩家选择 \n(超时未做选择, 则默认同意)' : '请问是否同意? (超时未做选择, 则默认同意)');

            var acceptCount = 0;
            var rejectCount = 0;
            for (var i = 0; i < arr.length; i++) {
                var player = arr[i];
                if (player.VoteValue == 2) {
                    //拒绝
                    $('root.info' + i + '.statusicon').setTexture(huzi.res.roomjiesan_no_png);
                    $('root.info' + i + '.status').setString('已拒绝');
                    rejectCount++;
                } else if (player.VoteValue == 1) {
                    //同意
                    $('root.info' + i + '.statusicon').setTexture(huzi.res.roomjiesan_ok_png);
                    $('root.info' + i + '.status').setString('已同意');
                    acceptCount++;
                } else {
                    //未选择
                    $('root.info' + i + '.statusicon').setTexture(huzi.res.roomjiesan_wait_png);
                    $('root.info' + i + '.status').setString('未选择');
                }
            }
            if (arr.length == 2) {
                $('root.info2').setVisible(false);
            }
            $('root.panel.t0').setString(content);

            $('root.panel.btn_ok').setVisible(false);
            if (haveISelected) {
                $('root.panel.btn_confirm').setVisible(false);
                $('root.panel.btn_cancel').setVisible(false);
            } else {
                $('root.panel.btn_confirm').setVisible(true);
                $('root.panel.btn_cancel').setVisible(true);
            }
            $('root.panel.result').setVisible(false);
            if (acceptCount == arr.length) {
                gameData.jiesanSucess = true;
                $('root.panel.btn_confirm').setVisible(false);
                $('root.panel.btn_cancel').setVisible(false);
                $('root.panel.btn_ok').setVisible(true);
                $('root.panel.result').setVisible(true);
                $('root.panel.result').setString('投票结果为: 解散房间');

                TouchUtil.setOnclickListener(this.btn_ok, function () {
                    if (that.parentlayer) {
                        that.parentlayer.showRoomClean();
                        // if(that.parentlayer.gameResultLayer.isVisible() || that.parentlayer.resultLayer.isVisible() || that.parentlayer.isReady){
                        //     that.parentlayer.showRoomClean();
                        // }
                    }
                    that.removeFromParent();
                });
            }
            if (rejectCount > 0) {
                this.timeLeft = 0;
                TouchUtil.setOnclickListener(this.btn_ok, function () {
                    that.removeFromParent();
                    mRoom.isPause = false;
                    if (gameData.switchVideo) {
                        AgoraUtil.showAllVideo();
                    }
                });
                $('root.panel.btn_confirm').setVisible(false);
                $('root.panel.btn_cancel').setVisible(false);
                $('root.panel.btn_ok').setVisible(true);
                $('root.panel.result').setVisible(true);
                $('root.panel.result').setString('投票结果为: 不解散房间');
            }
        },
    });
    exports.PhzRoomQuit = PhzRoomQuit;
})(window);
